﻿using Heroius.Files;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Ranpage
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Closing += MainWindow_Closing;

            BasePath = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\Heroius\\Ranpage\\";
            Directory.CreateDirectory(BasePath);
            ConfigPath = $"{BasePath}\\config";
            if (File.Exists(ConfigPath))
            {
                //load config
                EntitySilo silo = new EntitySilo();
                silo.Load(ConfigPath);
                silo.Fetch("config", Config);
                //auto load last src & rules
                if (!string.IsNullOrWhiteSpace(Config.LastSrc))
                {
                    if (File.Exists(Config.LastSrc))
                    {
                        Core.ImportSource(Config.LastSrc);
                    }
                }
                if (!string.IsNullOrWhiteSpace(Config.LastRule))
                {
                    if (File.Exists(Config.LastRule))
                    {
                        Core.ImportRules(Config.LastRule);
                    }
                }
            }

            dia_open_source = new OpenFileDialog() { Filter = "分组单|*.lst" };
            dia_save_source = new SaveFileDialog() { Filter = "分组单|*.lst" };
            dia_open_rules = new OpenFileDialog() { Filter = "分组规则|*.rule" };
            dia_save_rules = new SaveFileDialog() { Filter = "分组规则|*.rule" };
            dia_save_result = new SaveFileDialog() { Filter = "分组单|*.lst" };

            DataContext = Core;
        }

        string BasePath, ConfigPath;
        

        private void MainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            EntitySilo silo = new EntitySilo();
            silo.Store("config", Config);
            silo.Save(ConfigPath);
        }

        public Engine Core { get; set; } = new Engine();
        public Config Config { get; set; } = new Config();

        OpenFileDialog dia_open_source;
        private void Source_Import(object sender, RoutedEventArgs e)
        {
            if (dia_open_source.ShowDialog() == true)
            {
                Core.ImportSource(dia_open_source.FileName);
                Config.LastSrc = dia_open_source.FileName;
            }
        }
        SaveFileDialog dia_save_source;
        private void Source_Export(object sender, RoutedEventArgs e)
        {
            if (dia_save_source.ShowDialog() == true)
            {
                Core.ExportSource(dia_save_source.FileName);
                Config.LastSrc = dia_save_source.FileName;
            }
        }
        OpenFileDialog dia_open_rules;
        private void Rules_Import(object sender, RoutedEventArgs e)
        {
            if (dia_open_rules.ShowDialog() == true)
            {
                Core.ImportRules(dia_open_rules.FileName);
                Config.LastRule = dia_open_rules.FileName;
            }
        }
        SaveFileDialog dia_save_rules;
        private void Rules_Export(object sender, RoutedEventArgs e)
        {
            if (dia_save_rules.ShowDialog() == true)
            {
                Core.ExportRules(dia_save_rules.FileName);
                Config.LastRule = dia_save_rules.FileName;
            }
        }

        private void Rules_Go(object sender, RoutedEventArgs e)
        {
            Core.MainGo();
        }

        SaveFileDialog dia_save_result;
        private void Result_Export(object sender, RoutedEventArgs e)
        {
            if (dia_save_result.ShowDialog() == true)
            {
                Core.ExportResult(dia_save_result.FileName);
            }
        }
    }
}
